﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using SharpDiary.Core.Security;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Utility;

namespace SharpDiary.Diary
{
    public partial class DiaryItemPasswordView : UserControl
    {
        private DiaryInfo _diary;

        public DiaryInfo Diary
        {
            get { return _diary; }
            set { _diary = value; }
        }

        public DiaryItemPasswordView()
        {
            InitializeComponent();

            ResizeRedraw = true;
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            pnlCenter.Location = new Point((this.Width - pnlCenter.Width) / 2, (this.Height - pnlCenter.Height) / 2);
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            if (!Visible) { txtPassword.Clear(); }
        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);

            txtPassword.Focus();
            txtPassword.SelectAll();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (_diary == null) { return; }

            if (MD5Helper.IsEquals(txtPassword.Text, _diary.PrivatePasswordMD5))
            {
                this.Hide();
                Application.DoEvents();
            }
            else
            {
                MessageBoxHelper.ShowAlert("您输入的密码不正确。");
                txtPassword.Focus();
                txtPassword.SelectAll();
            }
        }
    }
}
